<?php
//
// Copyright ReccThis.com
// All Rights Reserved
//

class clsReccRecc extends cDycmsObject
{
    protected $mClsId = 'ReccRecc';

	protected $mBusiness;
	
	function Render(cDycmsClientNode &$r_client, $disp = true)
	{
		$r_client->AddChild(new cUiHeading($this->GetSummary()), 2);
		$r_client->AddChild($this->mAttributes['description']);
	}

	function DoEdit($vzAttrib){
		
		$biz = (isset($vzAttrib['business']) ? $vzAttrib['business']
				: $this->mAttributes['business']);
		
		if($biz){
			if(!is_numeric($biz))
				throw new dyExValidation('Invalid business ID');
			
			$this->mBusiness = dyLoadObject('ReccBusiness', $biz);
			if(!$this->mBusiness->Exists())
				throw new dyExNotFound('Supplied business ID does not match an existing business.');
			
			// Just set new tags
			$this->mBusiness->DoEdit(array('tags' => $vzAttrib['tags']));
		}else{
			
			//
			// Business does not exist, create a new one
			// using the details provided
			// TODO: run a query to find a possible match?
			//
			
			$this->mBusiness = dyCreateObject('ReccBusiness');
			$this->mBusiness->DoEdit($vzAttrib);
		};
		
		return parent::DoEdit(array(
			'by' => (!$this->mOwner->IsGuest() ? $vzAttrib['by'] : ''),
			'description' => $vzAttrib['description'],
			'business' => $this->mBusiness->GetId()
		));
	}
	
	function Write(){
		$this->mBusiness->Write();
		$this->mAttributes['business'] = $this->mBusiness->GetId();
		parent::Write();
	}
	
	function DrawEditor(cUiEditor &$editor)
	{
		global $gClient;
		
		//
		// Draw the editor form
		//

		$editor->AddChild(new cUiFormData('business',
			$this->mAttributes['business']));
		
		$editor->AddChild(new cUiHeading('What is it?'));
		
		$editor->AddChild('Enter the name of the business.');
		$editor->AddChild(new cUiTagEditor(array(
			'name' => 'name')));
			
		$editor->AddChild('Enter a comma separated list of short, snappy tags, '.
			'describing what the business does. For example: clothes, suit hire.');
		$editor->AddChild(new cUiTagEditor(array(
			'name' => 'tags')));
		
		
		$editor->AddChild(new cUiHeading('Where is it?'));
		
		$editor->AddChild('<em>Briefly</em> describe how to get there. If it is a '.
			'website, enter its URL here.');
		$editor->AddChild(new cUiTextBox(array(
			'name' => 'location')));
		
		$editor->AddChild('Enter the postcode, if you know it.');
		$editor->AddChild(new cUiTextBox(array(
			'name' => 'postcode')));
		
		$editor->AddChild(new cUiHeading('What did you think?'));
		$editor->AddChild('Describe why you are recommending this business.');
		$editor->AddChild(new cUiTextArea(array(
			'name' => 'description')));
		
		if(!$gClient->IsValidated()){
			
			//
			// The user isn't logged in, so ask them for their name and
			// display a captcha for security.
			//
			
			$editor->AddChild(new cUiHeading('Who are you?'));
			$editor->AddChild('Enter your name, to add a personal touch to your recommendation.');
			$editor->AddChild(new cUiTextBox(array(
				'name' => 'by')));
			
			$cc = new cUiCaptcha('ReccRecc');
			$cc->CreateCode();
			$editor->AddChild($cc);
		};
		
		$editor->AddChild(new cUiButton(array(
			'name' => '$$os-editorAction',
			'value' => 'saveNew'),
			'Recommend It!', 'building_add'));
	}
	
	function DrawPublisher(cUiPublisher &$rPublisher)
	{
		throw new dyExInvalidOp("Recommendations cannot be published");
	}
	
	function DoPublish()
	{
		throw new dyExInvalidOp("Recommendations cannot be published");
	}
	
	function GetSummary()
	{
    	return $this->GetBy() . ' on ' . $this->GetDate();
	}

	function LoadObject()
	{
        $this->mBusiness = null;
	}

    function Create(){
    	$this->mAttributes['business'] = 0;
		$this->mAttributes['by'] = '';
        $this->mAttributes['description'] = '';
		$this->mAttributes['date'] = time();
    }
	
	function GetBy(){
		return ($this->mOwner->IsGuest() ? $this->mAttributes['by']
			: $this->mOwner->GetUserName());
	}
	
	function GetDate(){
		return date("d M Y", $this->mAttributes['date']);
	}
}

class cCollectionReccRecc extends cDycmsAbstractCollection
{
	function Render(cDycmsClientNode &$rClient)
	{
		foreach($this->mzObject as $rec)
		{
			$rClient->AddChild(new cHtmlHyperlink($rec->GetUrl(),
				uiGetIcon('comment').$rec->GetSummary()));
		};
	}

	public function GetSummary($i = -1)
	{
		if($i > -1)
			return $this->mObject[$i]->GetSummary();

		$n = $this->Count();
		if($n == 1)
			return 'One recommendation';
		else
			return $n.' recommendations';
	}
}

?>
